<template>
	<view class="login-container">
		<!-- 鹿头图标 -->
		<image class="deer-icon" src="/static/下载1.webp"></image>
		<!-- 用户名输入框 -->
		<u--form class="input-item" labelPosition="left" :model="userInfo" :rules="rules" ref="uForm">
			<u-form-item class="label" label="姓名" prop="userName" borderBottom ref="item1">
				<u--input class="input" v-model="userInfo.userName" border="none"></u--input>
			</u-form-item>
			<!-- 密码输入框 -->
			<u-form-item class="label" label="密码" prop="password" borderBottom ref="item1">
				<u--input class="input" v-model="userInfo.password" border="none"></u--input>
			</u-form-item>
			<!-- 登录按钮 -->
			<u-form-item>
				<u-button class="login-btn" type="primary" @click="denglu">登录</u-button>
			</u-form-item>
			<!-- 注册按钮 -->
			<u-form-item>
				<button class="register-btn" type="default">注册</button>
			</u-form-item>
		</u--form>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import http from '../../http';
	export default {
		data() {
			return {
				userInfo: {
					userName: '',
					password: '',
					remember: false
				},
				rules: {
					'userName': {
						type: 'string',
						required: true,
						message: '请填写用户名',
						trigger: ['blur', 'change']
					},
					'password': {
						type: 'string',
						required: true,
						message: '请填写密码',
						trigger: ['blur', 'change']
					},
				},
			};
		},
		methods: {
			denglu() {
				this.$refs.uForm.validate().then(res => {
					console.log(this.userInfo);
					http({
						url: "/api/user/login",
						method: "post",
						data: this.userInfo
					}).then(res => {
						console.log(res);
						if (res.code === 1) {
							this.$refs.uToast.show({
								type: "success",
								message: "登陆成功",
								duration: 1000,
								complete() {
									uni.switchTab({
										url: "/pages/index/index"
									})
								}
							});
						} else {
							this.$refs.uToast.show({
								type: "error",
								message: res.message,
								duration: 1000,
							});
						}
					})
				})

			}
		}
	}
</script>

<style scoped>
	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		padding: 40rpx;
	}

	.deer-icon {
		width: 260rpx;
		height: 260rpx;
		margin-bottom: 40rpx;
	}

	.input-item {
		width: 100%;
		display: flex;
		flex-direction: column;
		margin-bottom: 30rpx;
	}

	.label {
		font-size: 28rpx;
		color: #333;
		margin-bottom: 10rpx;
	}

	.input {
		height: 60rpx;
		border: 1px solid #eee;
		border-radius: 8rpx;
		padding: 0 20rpx;
		font-size: 26rpx;
	}

	.login-btn {
		width: 100%;
		margin-bottom: 20rpx;
	}

	.register-btn {
		width: 100%;
	}
</style>